package com.the4man.cnbdw.action;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.EmployeePunchHistoryService;
import com.the4man.cnbdw.service.EmployeeService;
import com.the4man.cnbdw.service.PunchMachService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.cnbdw.utils.SelectModel;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.*;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Action(
        value = "PunchMachAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/punchMach/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/punchMach/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/punchMach/edit.jsp"
        )}
)
public class PunchMachAction extends BaseAction implements ModelDriven<PunchMachInfo> {
    private PunchMachInfo punchMach = new PunchMachInfo();
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private PunchMachService punchMachService;
    @Autowired
    private EmployeePunchHistoryService employeePunchHistoryService;

    public PunchMachAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        return "addPage";
    }

    public String toEditPage() {
        try {
            HttpServletRequest request = this.getRequest();
            PunchMachInfo pm = (PunchMachInfo)this.punchMachService.selectByPrimaryKey(this.getRequest().getParameter("id"), PunchMachInfo.class);
            request.setAttribute("punchMachNo", pm.getPunchMachNo());
            request.setAttribute("type", pm.getType());
            request.setAttribute("id", pm.getId());
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            PunchMachInfoExample punchMachInfoExample = new PunchMachInfoExample();
            if(StringUtils.isNotEmpty(this.getRequest().getParameter("punchMachNo"))) {
                punchMachInfoExample.createCriteria().andNameLike("%" + this.getRequest().getParameter("punchMachNo") + "%");
            }

            int total = this.punchMachService.countByExample(punchMachInfoExample);
            Page<PunchMachInfo> page = new Page();
            this.setPageProperties(page);
            this.punchMachService.selectByExample(punchMachInfoExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal(Long.valueOf((long)total));
            this.writeJson(d);
        } catch (Exception var5) {
            LOG.error("获取数据失败", var5, new String[0]);
        }

    }

    public void add() {
        PageJson json = new PageJson();

        try {
            HttpServletRequest request = this.getRequest();
            String name = request.getParameter("punchMachNo");
            String type = request.getParameter("type");
            PunchMachInfo pm = this.punchMachService.selectByName(name);
            if(pm == null) {
                this.punchMach.setId(UUID.randomUUID().toString());
                this.punchMach.setPunchMachNo(name);
                this.punchMach.setType(type);
                this.punchMachService.insert(this.punchMach);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("打卡机名已存在");
            }
        } catch (Exception var6) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var6, new String[0]);
        }

        this.writeJson(json);
    }

    public void update() {
        PageJson json = new PageJson();

        try {
            HttpServletRequest request = this.getRequest();
            String name = request.getParameter("punchMachNo");
            String id = request.getParameter("id");
            String type = request.getParameter("type");
            PunchMachInfo pm = this.punchMachService.selectMByName(id, name);
            if(pm == null) {
                this.punchMach.setId(this.getRequest().getParameter("id"));
                this.punchMach.setPunchMachNo(name);
                this.punchMach.setType(type);
                this.punchMachService.updateByPrimaryKey(this.punchMach);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("打卡机名已存在");
            }
        } catch (Exception var7) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var7, new String[0]);
        }

        this.writeJson(json);
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int count = 0;
            String ids = this.getRequest().getParameter("ids");
            if(ids.contains(",")) {
                String[] str = ids.split(",");
                String[] var8 = str;
                int var7 = str.length;

                for(int var6 = 0; var6 < var7; ++var6) {
                    String info = var8[var6];
                    int i = this.punchMachService.deleteByPrimaryKey(info, PunchMachInfo.class);
                    if(i > 0) {
                        ++count;
                    }
                }
            } else {
                count = this.punchMachService.deleteByPrimaryKey(ids, PunchMachInfo.class);
            }

            if(count > 0) {
                json.setReturnMsg("成功删除" + count + "个信息！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var10) {
            LOG.error("批量删除失败！", var10, new String[0]);
            json.setReturnMsg("删除失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void getPunchMachType() {
        HttpServletRequest request = this.getRequest();
        String punchMachNo = request.getParameter("punchMachNo");
        PunchMachInfo punchMach = this.getPunchMachType(punchMachNo);
        PageJson resultJson = new PageJson();

        try {
            if(punchMach == null) {
                resultJson.setReturnMsg("打卡机未识别,cardNo>>>>[" + punchMachNo + "]");
                this.writeJson(resultJson);
                return;
            }

            JSONObject json = new JSONObject();
            json.put("type", punchMach.getType());
            resultJson.setReturnFlag(true);
            resultJson.setReturnMsg("成功!");
            resultJson.setObj(json);
        } catch (Exception var6) {
            var6.printStackTrace();
            resultJson.setReturnMsg(var6.getMessage());
        }

        this.writeJson(resultJson);
    }

    private PunchMachInfo getPunchMachType(String punchMachNo) {
        PunchMachInfo result = null;

        try {
            List<PunchMachInfo> list = this.punchMachService.getPunchMachType(punchMachNo);
            if(list != null && list.size() > 0) {
                result = (PunchMachInfo)list.get(0);
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return result;
    }

    public void getCardInfo() {
        HttpServletRequest request = this.getRequest();
        String cardNum = request.getParameter("cardNum");
        String punchMachNo = request.getParameter("punchMachNo");
        String type = this.getPunchMachType(punchMachNo).getType();
        List<Employee> emp = this.employeeService.getAllDataByCardNum(cardNum);
        if(emp != null && emp.size() != 0) {
            Employee empl = (Employee)emp.get(0);
            PageJson resultJson = new PageJson();
            JSONObject json = new JSONObject();
            json.put("name", ((Employee)emp.get(0)).getName());
            json.put("cardType", ((Employee)emp.get(0)).getCardType());
            json.put("cardNum", ((Employee)emp.get(0)).getCardNo());
            json.put("unit", ((Employee)emp.get(0)).getCompany());
            json.put("photoPath", ((Employee)emp.get(0)).getPhoto());
            String time = DateUtil.getInstance().formatDate(new Date());

            try {
                String namePy;
                Employee emplo;
                EmployeePunchHistory eph;
                if(type != "1" && !"1".equals(type)) {
                    if(type != "2" && !"2".equals(type)) {
                        if(type == "3" || "3".equals(type)) {
                            if(empl.getPunch().shortValue() == 0) {
                                empl.setPunch(Short.valueOf("1"));
                                empl.setPunchTime(time);
                                this.employeeService.updateByPrimaryKey(empl);
                                namePy = empl.getNamePy();
                                if(namePy != null) {
                                    emplo = new Employee();
                                    emplo.setId(UUID.randomUUID().toString());
                                    emplo.setName(empl.getName());
                                    emplo.setNamePy("1");
                                    emplo.setCreateTime(DateUtil.getInstance().formatDate(new Date()));
                                    this.employeeService.insertMonitorEmp(emplo);
                                }

                                eph = new EmployeePunchHistory();
                                eph.setId(UUID.randomUUID().toString());
                                eph.setEmpId(empl.getId());
                                eph.setPunchTime(time);
                                eph.setPunch(Short.valueOf("1"));
                                eph.setOperateId("");
                                eph.setPunchMachNo(punchMachNo);
                                eph.setCardNum(cardNum);
                                this.employeePunchHistoryService.insert(eph);
                                resultJson.setReturnMsg("上船成功！");
                                resultJson.setReturnFlag(true);
                                resultJson.setObj(json);
                                this.writeJson(resultJson);
                            } else if(empl.getPunch().shortValue() == 1) {
                                empl.setPunch(Short.valueOf("0"));
                                empl.setPunchTime(time);
                                this.employeeService.updateByPrimaryKey(empl);
                                namePy = empl.getNamePy();
                                if(namePy != null) {
                                    emplo = new Employee();
                                    emplo.setId(UUID.randomUUID().toString());
                                    emplo.setName(empl.getName());
                                    emplo.setNamePy("2");
                                    emplo.setCreateTime(DateUtil.getInstance().formatDate(new Date()));
                                    this.employeeService.insertMonitorEmp(emplo);
                                }

                                eph = new EmployeePunchHistory();
                                eph.setId(UUID.randomUUID().toString());
                                eph.setEmpId(empl.getId());
                                eph.setPunchTime(time);
                                eph.setPunch(Short.valueOf("0"));
                                eph.setOperateId("");
                                eph.setPunchMachNo(punchMachNo);
                                eph.setCardNum(cardNum);
                                this.employeePunchHistoryService.insert(eph);
                                resultJson.setReturnMsg("下船成功！");
                                resultJson.setReturnFlag(true);
                                resultJson.setObj(json);
                                this.writeJson(resultJson);
                            }
                        }
                    } else {
                        empl.setPunch(Short.valueOf("0"));
                        empl.setPunchTime(time);
                        this.employeeService.updateByPrimaryKey(empl);
                        namePy = empl.getNamePy();
                        if(namePy != null) {
                            emplo = new Employee();
                            emplo.setId(UUID.randomUUID().toString());
                            emplo.setName(empl.getName());
                            emplo.setNamePy("2");
                            emplo.setCreateTime(DateUtil.getInstance().formatDate(new Date()));
                            this.employeeService.insertMonitorEmp(emplo);
                        }

                        eph = new EmployeePunchHistory();
                        eph.setId(UUID.randomUUID().toString());
                        eph.setEmpId(empl.getId());
                        eph.setPunchTime(time);
                        eph.setPunch(Short.valueOf("0"));
                        eph.setOperateId("");
                        eph.setPunchMachNo(punchMachNo);
                        eph.setCardNum(cardNum);
                        this.employeePunchHistoryService.insert(eph);
                        resultJson.setReturnMsg("下船成功！");
                        resultJson.setReturnFlag(true);
                        resultJson.setObj(json);
                        this.writeJson(resultJson);
                    }
                } else {
                    empl.setPunch(Short.valueOf("1"));
                    empl.setPunchTime(time);
                    this.employeeService.updateByPrimaryKey(empl);
                    namePy = empl.getNamePy();
                    if(namePy != null) {
                        emplo = new Employee();
                        emplo.setId(UUID.randomUUID().toString());
                        emplo.setName(empl.getName());
                        emplo.setNamePy("1");
                        emplo.setCreateTime(DateUtil.getInstance().formatDate(new Date()));
                        this.employeeService.insertMonitorEmp(emplo);
                    }

                    eph = new EmployeePunchHistory();
                    eph.setId(UUID.randomUUID().toString());
                    eph.setEmpId(empl.getId());
                    eph.setPunchTime(time);
                    eph.setPunch(Short.valueOf("1"));
                    eph.setOperateId("");
                    eph.setPunchMachNo(punchMachNo);
                    eph.setCardNum(cardNum);
                    this.employeePunchHistoryService.insert(eph);
                    resultJson.setReturnMsg("上船成功！");
                    resultJson.setReturnFlag(true);
                    resultJson.setObj(json);
                    this.writeJson(resultJson);
                }
            } catch (Exception var13) {
                this.writeJson(var13.toString());
                var13.printStackTrace();
            }
        } else {
            PageJson resultJson = new PageJson();
            resultJson.setReturnMsg("人员不存在！");
            resultJson.setReturnFlag(false);
            this.writeJson(resultJson);
        }

    }

    public void getpunchmachtype() {
        try {
            List<SelectModel> list = new ArrayList();
            list.add(new SelectModel("1", "上船"));
            list.add(new SelectModel("2", "下船"));
            list.add(new SelectModel("3", "上下船"));
            this.writeJson(list);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public PunchMachInfo getModel() {
        return this.punchMach;
    }
}
